package com.yonyou.marketing.app.api.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.marketing.common.Response;
import org.marketing.common.constants.ExceptionMessage;
import org.marketing.common.exception.ServiceBizException;
import org.marketing.common.util.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.marketing.app.api.dto.NaireCommit;

@Service("appQuestionnaireService")
public class AppQuestionnaireService {
	
	private Logger          logger = Logger.getLogger(AppQuestionnaireService.class);
	
	@Autowired
    private QuestionnaireServiceInterface    questionnaireServiceInterface;

	public List<Map<String, Object>> questionType(Integer queryType,String userId){
		logger.info("调用调查问券服务  SERVICE-QUESTIONNAIRE==>start...");
		Response<List<Map<String, Object>>> response = questionnaireServiceInterface.questionType(queryType, userId);
		return response.getData();
	}
	
	public Map<String, Object> explainView(Integer qneId){
		logger.info("调用调查问券服务  SERVICE-QUESTIONNAIRE==>start...");
		Response<Map<String, Object>> response = questionnaireServiceInterface.explainView(qneId);
		return response.getData();
	}
	
	@SuppressWarnings("unchecked")
	public Map<String, Object> questionNaireDetail(Integer qneId,String userId){
		logger.info("调用调查问券服务  SERVICE-QUESTIONNAIRE==>start...");
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> questionList = new ArrayList<>();
		Map<String, List<Map<String, Object>>> answerList = new HashMap<>();
		Response<Map<String, Object>> response = questionnaireServiceInterface.questionNaireDetail(qneId, userId);
		Map<String, Object> resultMap = response.getData();
		List<Map<String, Object>> list = (List<Map<String, Object>>) resultMap.get("pages");
		if(!CheckUtil.NullOrEmpty(list)){
			for (Map<String, Object> map : list) {
				List<Map<String, Object>> qList = (List<Map<String, Object>>) map.get("questions");
				Map<String, Object> qMap = qList.get(0);
				String name = qMap.get("name").toString();
				String type = qMap.get("type").toString();
				if("checkbox".equals(type)){
					qMap.put("type", "1");
				}else if("radiogroup".equals(type)){
					qMap.put("type", "2");
				}else if("comment".equals(type)){
					qMap.put("type", "3");
				}
				List<Map<String, Object>> aList = (List<Map<String, Object>>) qMap.get("choices");
				qMap.remove("choices");
				questionList.add(qMap);
				answerList.put(name, aList);
			}
		}
		result.put("questionList", questionList);
		result.put("answerList", answerList);
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public Map<String, Object> questionResultDetail(Integer qneId,String userId){
		logger.info("调用调查问券服务  SERVICE-QUESTIONNAIRE==>start...");
		Response<Map<String, Object>> response = questionnaireServiceInterface.questionResultDetail(qneId, userId);
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> questionList = new ArrayList<>();
		Map<String, List<Map<String, Object>>> answerList = new HashMap<>();
		Map<String, Object> resMap = response.getData();
		Map<String, Object> pgMap = (Map<String, Object>) resMap.get("pg");
		List<Map<String, Object>> list = (List<Map<String, Object>>) pgMap.get("pages");
		if(!CheckUtil.NullOrEmpty(list)){
			for (Map<String, Object> map : list) {
				List<Map<String, Object>> qList = (List<Map<String, Object>>) map.get("questions");
				Map<String, Object> qMap = qList.get(0);
				String name = qMap.get("name").toString();
				String type = qMap.get("type").toString();
				if("checkbox".equals(type)){
					qMap.put("type", "1");
				}else if("radiogroup".equals(type)){
					qMap.put("type", "2");
				}else if("comment".equals(type)){
					qMap.put("type", "3");
				}
				List<Map<String, Object>> aList = (List<Map<String, Object>>) qMap.get("choices");
				qMap.remove("choices");
				questionList.add(qMap);
				answerList.put(name, aList);
			}
		}
		Map<String, Object> asMap = (Map<String, Object>) resMap.get("as");
		result.put("questionList", questionList);
		result.put("answerList", answerList);
		result.put("userAnswerList", asMap);
		return result;
	}
	
	public Map<String, Object> commitNaire(NaireCommit commit){
		if(CheckUtil.NullOrEmpty(commit.getUserId())){
			throw new ServiceBizException(ExceptionMessage.USERID_IS_NULL);
		}
		logger.info("调用调查问券服务  SERVICE-QUESTIONNAIRE==>start...");
		Response<Map<String, Object>> response = questionnaireServiceInterface.commitNaire(commit, commit.getUserId());
		return response.getData();
	}
}
